﻿using Electronic_work_order.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace Electronic_work_order.Interfacess.OA_Permission
{
    public class Sql_Permission : Interface_Permission
    {
        private readonly AppDbContext _context;

        public Sql_Permission(AppDbContext context)
        {
            _context = context;
        }

        //添加角色
        public int AddUserRole(UserRole role)
        {   

            _context.UserRoles.Add(role);
           return _context.SaveChanges();
        }

        //查询权限+状态（链表）
        public IEnumerable<Permission> GetPermissions()
        {
         return  _context.permissions.Include(a => a.PermissionState).Where(v=>v.PermissionID!=1).ToList();
        }


        //查询角色表
        public IEnumerable<UserRole> GetRoles()
        {
            return _context.UserRoles.ToList();
        }
        //角色模糊查询
        public IEnumerable<UserRole> GetRoles_MoHu(string RoleName)
        {
          return  _context.UserRoles.Where(a => a.RoleName == RoleName).ToList();
        }
        //修改状态（禁用）
        public int Update_JinYong_Sate(int PermissonID)
        {
            Permission p = _context.permissions.Include(a => a.PermissionState).Where(a => a.PermissionID == PermissonID).FirstOrDefault();
            if (p.PermissionStateID == 2)
            {
                p.PermissionStateID = 1;
            }
            return _context.SaveChanges();
        }

        //修改状态（启用）
        public int Update_QiYong_Sate(int PermissonID)
        {
          Permission p=  _context.permissions.Include(a => a.PermissionState).Where(a => a.PermissionID == PermissonID).FirstOrDefault();
            if (p.PermissionStateID==1)
            {
                p.PermissionStateID = 2;
            }
            
           return _context.SaveChanges();
        }
    }
}
